package org.CurrencyDAO.entity;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class JoinFetchTest {

	private static SessionFactory sessionFactory;

	public JoinFetchTest() {
		sessionFactory = new Configuration().configure().buildSessionFactory();
	}

	public void test1() {

		System.out.println("TEST 1");
		
		Session openSession = sessionFactory.openSession();

		Query namedQuery = openSession.getNamedQuery("CurrencyExchange.wszystko");
		List<CurrencyExchange> list = namedQuery.list();
		
//
//		for (CurrencyExchange currencyExchange : list) {
//			System.out.println(currencyExchange);
//		}
		for (CurrencyExchange currencyExchange : list) {
			System.out.println(currencyExchange.getCurrency().getDescription());
		}
		
		openSession.close();
	}

	public void test2() {

		System.out.println("TEST 2");
		
		Session openSession = sessionFactory.openSession();

		Query namedQuery = openSession.getNamedQuery("CurrencyExchange.poIdJOIN_FETCH").setParameter("id", 214l);
		List<CurrencyExchange> list = namedQuery.list();
		
		for (CurrencyExchange currencyExchange : list) {
			System.out.println(currencyExchange);
		}
		for (CurrencyExchange currencyExchange : list) {
			System.out.println(currencyExchange.getCurrency().getDescription());
		}
		
	}

	
	public void test3() {

		System.out.println("TEST 3");
		
		Session openSession = sessionFactory.openSession();

		Query namedQuery = openSession.getNamedQuery("CurrencyExchange.poIdLEFT_JOIN_FETCH").setParameter("id", 214l);
		List<CurrencyExchange> list = namedQuery.list();
		openSession.close();

		for (CurrencyExchange currencyExchange : list) {
			System.out.println(currencyExchange);
		}
		for (CurrencyExchange currencyExchange : list) {
			System.out.println(currencyExchange.getCurrency().getDescription());
		}
	}
	public static void main(String[] args) {

		JoinFetchTest fetchTest = new JoinFetchTest();
		fetchTest.test1();
//		fetchTest.test2();
//		fetchTest.test3();z

	}

}
